<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<meta name="renderer" content="webkit|ie-comp|ie-stand">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
<!--[if lt IE 9]>
<script type="text/javascript" src="lib/html5shiv.js"></script>
<script type="text/javascript" src="lib/respond.min.js"></script>
<![endif]-->
<link rel="stylesheet" type="text/css" href="DataTables/datatables.min.css"/>
<link rel="stylesheet" type="text/css" href="static/h-ui/css/H-ui.min.css" />
<link rel="stylesheet" type="text/css" href="static/h-ui.admin/css/H-ui.admin.css" />
<link rel="stylesheet" type="text/css" href="lib/Hui-iconfont/1.0.8/iconfont.css" />
<link rel="stylesheet" type="text/css" href="static/h-ui.admin/skin/default/skin.css" id="skin" />
<link rel="stylesheet" type="text/css" href="static/h-ui.admin/css/style.css" />
<!--[if IE 6]>
<script type="text/javascript" src="lib/DD_belatedPNG_0.0.8a-min.js" ></script>
<script>DD_belatedPNG.fix('*');</script>
<![endif]-->
<title>清分业务统计报表</title>
<style>
	table th{
		text-align: center;
	}
	tfoot th{
		text-align: left;
	}
</style>
</head>
<body>
<nav class="breadcrumb"><i class="Hui-iconfont">&#xe67f;</i> 首页
	<span class="c-gray en">&gt;</span>
	统计分析
	<span class="c-gray en">&gt;</span>
	清分业务统计报表
	<a class="btn btn-success radius r" style="line-height:1.6em;margin-top:3px" href="javascript:location.replace(location.href);" title="刷新" ><i class="Hui-iconfont">&#xe68f;</i></a>
</nav>
<div class="page-container" id="app">
	<div class="text-c">
		<form id="queryForm">
			<span> 清分日期：
			<input type="text" onfocus="WdatePicker({ maxDate:'#F{$dp.$D(\'endDate\')||\'%y-%M-%d\'}' })" id="startDate" name="startDate" class="input-text Wdate" style="width:120px;">
			-
			<input type="text" onfocus="WdatePicker({ minDate:'#F{$dp.$D(\'startDate\')}',maxDate:'%y-%M-%d' })" id="endDate" name="endDate" class="input-text Wdate" style="width:120px;">
			</span>
			<span class="select-box inline" v-if="isAdmin">
				<select class="select" id="companyid" name="companyid"></select>
			</span>
			<button class="btn btn-success" type="button" v-on:click="query()"><i class="Hui-iconfont">&#xe665;</i> 搜索</button>
		</form>
	</div>
	<div class="mt-20">
		<table id="list" class="table table-border table-bordered table-hover table-bg table-sort" style="white-space: nowrap;">
			<tfoot>
				<tr></tr>
			</tfoot>
		</table>
	</div>
</div>
<!--_footer 作为公共模版分离出去-->
<script type="text/javascript" src="DataTables/datatables.js"></script>
<script type="text/javascript" src="lib/layer/2.4/layer.js"></script>
<script type="text/javascript" src="static/h-ui/js/H-ui.min.js"></script>
<script type="text/javascript" src="static/h-ui.admin/js/H-ui.admin.js"></script> <!--/_footer 作为公共模版分离出去-->

<!--请在下方写此页面业务相关的脚本-->
<script type="text/javascript" src="lib/My97DatePicker/4.8/WdatePicker.js"></script>
<script type="text/javascript" src="lib/laypage/1.2/laypage.js"></script>
<script type="text/javascript" src="js/jquery.base64.js"></script>
<script type="text/javascript" src="js/vue.js" charset="utf-8"></script>
<script type="text/javascript">
/*系统-车队-添加*/
function system_category_add(title,url,w,h){
	layer_show(title,url,w,h);
}
/*系统-车队-编辑*/
function system_category_edit(title,url,id,w,h){
	layer_show(title,url,w,h);
}
/*系统-车队-删除*/
function system_category_del(obj,id){
	layer.confirm('确认要删除吗？',function(index){
		$.ajax({
			type: 'POST',
			url: '',
			beforeSend: function (request) {
				request.setRequestHeader("Authorization", "Bearer " + getToken());
			},
			dataType: 'json',
			success: function(data){
				$(obj).parents("tr").remove();
				layer.msg('已删除!',{icon:1,time:1000});
			},
			error:function(data) {
				console.log(data.msg);
			},
		});
	});
}

</script>

<script type="text/javascript">
	$(function(){
		$.ajax({
			url: ipAddress + 'base/company/getList',
			// url: 'http://localhost:8080/base/company/getList',
			data: {
				order: "ordercode"
			},
			beforeSend: function (request) {
				request.setRequestHeader("Authorization", "Bearer " + getToken());
			},
			method:'GET',
			success:function (data,status,request) {
				$("#companyid").append("<option value>请选择分公司</option>");
				for(var i = 0; i < data.length; i ++){
					$("#companyid").append("<option value='" + data[i].companyid + "'>" + data[i].companyname + "</option>");
				}
			},
			error:function (request, msg, obj) {
				var respTxtObj = JSON.parse(request.responseText);
				console.log(respTxtObj);
				console.log(respTxtObj.error);
				console.log(respTxtObj.error_description);
			}
		});
	});
	
	var vm = new Vue({
		el : "#app",
		data : {
			msg : "",
			isAdmin : true
		},
		//在实例初始化之后，数据观测 (data observer) 和 event/watcher 事件配置之前被调用。
		beforeCreate:function(){
			console.log('beforeCreate');
		},
		/* 在实例创建完成后被立即调用。
		在这一步，实例已完成以下的配置：数据观测 (data observer)，属性和方法的运算，watch/event 事件回调。
		然而，挂载阶段还没开始，$el 属性目前不可见。 */
		created	:function(){
			this.isAdmin = hasRole('ADMIN');
			console.log('created');
		},
		//在挂载开始之前被调用：相关的渲染函数首次被调用
		beforeMount : function(){
			console.log('beforeMount');
		},
		//el 被新创建的 vm.$el 替换, 挂在成功	
		mounted : function(){
			console.log('mounted');
		},
		//数据更新时调用
		beforeUpdate : function(){
			console.log('beforeUpdate');
		},
		//组件 DOM 已经更新, 组件更新完毕 
		updated : function(){
			console.log('updated');
		},
		methods: {
			query: function(){
				table.draw();
			}
		}
	});
	
	$('#startDate').val(today());
	$('#endDate').val(today());
	
	var isInitFooter = true;
	
	function initFooter(tfoot, api) {
		
		function addCol(rIndex, cIndex, colSpan, text, fontSize) {
			var row = tfoot.parentElement.children[rIndex];
			var col;
			var isNewRow = false;
			var isNewCol = false;
			if(row == undefined) {
				row = document.createElement('tr');
				isNewCol = true;
				isNewRow = true;
			}
			if(cIndex == -1 || row.children[cIndex] == undefined){
				isNewCol = true;
			}
			//生成一个cell
			col = document.createElement('th');
			col.colSpan = colSpan != 0 ? colSpan : 1;
			col.style.whiteSpace = 'nowrap';
			col.style.fontSize = fontSize != null ? fontSize : '12px';
			col.innerHTML = text != null ? text : '';
			row.appendChild(col);
			if(isNewRow) {
				tfoot.parentElement.appendChild(row, tfoot);
			}
			if(isNewCol) {
				row.appendChild(col);
			} else {
				row.replaceChild(col, row.children[cIndex]);
			}
		}
		addCol(0, 0, -1, '合计');
		
		addCol(0, 1, -1, total == undefined ? '' : total.boxBagRecord.checkedCashboxAmount);
		addCol(0, 2, -1, total == undefined ? '' : total.boxBagRecord.checkedTMoneybagAmount);
		addCol(0, 3, -1, total == undefined ? '' : total.boxBagRecord.checkedRMoneybagAmount);
		
		addCol(0, 4, -1, total == undefined ? '' : total.checkedRecord.checkedSelfsvcMoney.toFixed(2));
		addCol(0, 5, -1, total == undefined ? '' : total.checkedRecord.checkedTicketMoney.toFixed(2));
		addCol(0, 6, -1, total == undefined ? '' : total.checkedRecord.checkedTotalMoney.toFixed(2));
		addCol(0, 7, -1, total == undefined ? '' : total.checkedRecord.checkedRechargeMoney.toFixed(2));
		addCol(0, 8, -1, total == undefined ? '' : (total.checkedRecord.checkedTotalMoney + total.checkedRecord.checkedRechargeMoney).toFixed(2));
		
		addCol(0, 9, -1, total == undefined ? '' : total.managedRecord.workers);
		
		addCol(1, 0, -1, '日均');
		
		addCol(1, 1, -1, avg == undefined ? '' : avg.boxBagRecord.checkedCashboxAmount);
		addCol(1, 2, -1, avg == undefined ? '' : avg.boxBagRecord.checkedTMoneybagAmount);
		addCol(1, 3, -1, avg == undefined ? '' : avg.boxBagRecord.checkedRMoneybagAmount);
		
		addCol(1, 4, -1, avg == undefined ? '' : avg.checkedRecord.checkedSelfsvcMoney.toFixed(2));
		addCol(1, 5, -1, avg == undefined ? '' : avg.checkedRecord.checkedTicketMoney.toFixed(2));
		addCol(1, 6, -1, avg == undefined ? '' : avg.checkedRecord.checkedTotalMoney.toFixed(2));
		addCol(1, 7, -1, avg == undefined ? '' : avg.checkedRecord.checkedRechargeMoney.toFixed(2));
		addCol(1, 8, -1, avg == undefined ? '' : (avg.checkedRecord.checkedTotalMoney + avg.checkedRecord.checkedRechargeMoney).toFixed(2));
		
		addCol(1, 9, -1, avg == undefined ? '' : avg.managedRecord.workers);
	}
	
	function updateHeaderCell(rIndex, cIndex, text) {
		if(document.getElementById('headR' + rIndex + 'C' + cIndex) == null) {
			return;
		} else {
			document.getElementById('headR' + rIndex + 'C' + cIndex).innerHTML = text;
		}
	}
	
	var isInitHeader = true;
	function initHeader(thead) {
		if(isInitHeader){
			isInitHeader = false;
		} else {
			return;
		}
		
		var rows = [];
		
		function addRows(rIndex, cIndex, rowSpan, colSpan, text, fontSize) {
			var isNewRow = false;
			var isNewCol = false;
			var row = rows[rIndex];
			var col;
			if(row == undefined) {
				//生成表头
				row = document.createElement('tr');
				row.setAttribute('role', 'row');
				isNewRow = true;
				isNewCol = true;
			}
			if(cIndex == -1 || row.children[cIndex] == undefined){
				isNewCol = true;
			}
			//生成一个cell
			col = document.createElement('th');
			col.id = 'headR' + rIndex + 'C' + cIndex;
			col.className = 'sorting_disabled';
			col.rowSpan = rowSpan != 0 ? rowSpan : 1;
			col.colSpan = colSpan != 0 ? colSpan : 1;
			col.style.textAlign = 'center';
			col.style.whiteSpace = 'nowrap';
			col.style.fontSize = fontSize != null ? fontSize : '12px';
			col.innerHTML = text != null ? text : '';
			row.appendChild(col);
			if(isNewCol) {
				row.appendChild(col);
			} else {
				row.replaceChild(col, row.children[cIndex]);
			}
			if(isNewRow) {
				rows.push(row);
			} else {
				rows[rIndex] = row;
			}
		}
		
		function appendRows() {
			for(var i = 0; i < rows.length; i ++) {
				thead.parentElement.insertBefore(rows[i], thead);
			}
		}
		
		addRows(0, 0, 0, 4, ' 统计周期:' + $('#startDate').val() + '至' + $('#endDate').val());
		addRows(0, 1, 0, 5, '');
		var username = getUserInfo().username;
		addRows(0, 2, 0, 3, '制表人:' + username);
		
		addRows(1, 0, 0, 1, '');
		addRows(1, 1, 0, 3, '处理货物统计');
		addRows(1, 2, 0, 5, '清点金额统计');
		addRows(1, 3, 0, 1, '工作效率统计');
		
		appendRows();
	}
	
	function formatSeconds(seconds) {
		if(seconds == undefined) {
			return '';
		} else {
			seconds = parseInt(seconds);
		}
		var minutes = 0;
		var hours = 0;
		if(seconds > 60) {
			minutes = parseInt(seconds / 60);
			seconds = parseInt(seconds % 60);
			if(minutes > 60) {
				hours = parseInt(minutes / 60);
				minutes = parseInt(minutes % 60);
			}
		}
		var time = '';
		time += (hours < 10 ? '0' + hours : hours) + ":";
		time += (minutes < 10 ? '0' + minutes : minutes) + ":";
		time += seconds < 10 ? '0' + seconds : seconds;
		return time;
	}
	
	//footer日均
	var avg;
	
	//footer合计
	var total;
	
	var table = $('#list').DataTable({
		language: {
			url: 'static/h-ui.admin/js/de_DE.txt'
		},
		// autoWidth: false,
		//stateSave: true,
		"lengthMenu":[10, 50, 100],
		"pageLength": 100,
		searching: false,
		ordering: false,
		processing: true,
		serverSide: true,
		headerCallback: function(thead, data, start, end, display) {
			initHeader(thead);
		},
		footerCallback: function (tfoot, data, start, end, display) {
			initFooter(tfoot, this.api());
			total = null;
		},
		ajax: {
			url: ipAddress + 'demo/clearingReport/getBusinessTablePage',
			type: 'GET',
			data: function ( data ) {
				var param = {};
				var userInfo = getUserInfo();
				var page = data.start / data.length + 1;
				var size = data.length;
				// var order = data.columns[data.order[0].column].data + " " + data.order[0].dir;
				param.draw = data.draw;
				param.page = page;
				param.size = size;
				param.order = 'companyid';
				var formData = $("#queryForm").serializeArray();//把form里面的数据序列化成数组
				formData.forEach(function (e) {
					param[e.name] = e.value;
				});
				if(hasRole('COMPANY')){
					param.companyId = userInfo.company;
				}
				return param;
			},
			dataFilter: function( data ) {
				var json = jQuery.parseJSON( data );
				avg = json.avg;
				total = json.total;
				updateHeaderCell(0, 0, ' 统计周期:' + $('#startDate').val() + '至' + $('#endDate').val());
				return JSON.stringify( json ); // return JSON string
			}
		},
		columns: [
			{title: '单位', name: 'companyname', data: 'companyname', defaultContent: '无'},
			
			{title: '清点<br>胆数', name: 'checkedCashboxAmount', data: 'boxBagRecord.checkedCashboxAmount', defaultContent: '0'},
			{title: '有人售<br>款包数', name: 'checkedTMoneybagAmount', data: 'boxBagRecord.checkedTMoneybagAmount', defaultContent: '0'},
			{title: '充值<br>款包数', name: 'checkedRMoneybagAmount', data: 'boxBagRecord.checkedRMoneybagAmount', defaultContent: '0'},
			
			{title: '无人售<br>金额', name: 'checkedSelfsvcMoney', data: 'checkedRecord.checkedSelfsvcMoney', defaultContent: '0'},
			{title: '有人售<br>金额', name: 'checkedTicketMoney', data: 'checkedRecord.checkedTicketMoney', defaultContent: '0'},
			{title: '票款<br>小计', name: 'checkedTotalMoney', data: 'checkedRecord.checkedTotalMoney', defaultContent: '0'},
			{title: '充值款<br>金额', name: 'checkedRechargeMoney', data: 'checkedRecord.checkedRechargeMoney', defaultContent: '0'},
			{title: '总金额<br>合计', name: 'totalMoney', defaultContent: '0', 
				render: function(data, type, full, meta) {
					var total = full.checkedRecord.checkedTotalMoney + full.checkedRecord.checkedRechargeMoney;
					return total;
				},
			},
			
			{title: '清分中心<br>清点人数', name: 'workers', data: 'managedRecord.workers', defaultContent: '0'}
			
		],
		columnDefs: [
			// {
			// 	'targets': 0,
			// 	'searchable': false,
			// 	'orderable': false,
			// 	'className': 'dt-body-center',
			// 	'render': function (data, type, full, meta){
			// 		return '<input type="checkbox" name="id[]" value="' + $('<div/>').text(data).html() + '">';
			// 	 }
			// },
			// {"orderable":false,"targets":[]},
			// {"visible":false,"targets":[0]}
		]
	});
	
	// table.column( 0 ).visible( false );
</script>
</body>
</html>